﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Twi.COMMON.Core
{
    /*
    * Copyright: ©2013 Tyingsoft 踏影软件开发工作室版权所有
    * Author: xuzhihong
    * Create Date:2013-04-26 
    * Description:条件查询辅助类     
    */
    public class TwiConditionHelper
    {
        /// <summary>
        /// 获取简单条件查询SQL语句
        /// </summary>
        /// <param name="condition">SimpleCondition</param>
        /// <returns></returns>
        public static string GetSimpleConditionSqlItem(SimpleCondition condition)
        {
            return GetSimpleConditionSqlItem(condition.Field, condition.Value, condition.COperator);
        }

        /// <summary>
        /// 获取简单条件查询SQL语句(拆分SimpleCondition属性为条件)
        /// </summary>
        /// <param name="conditionField"></param>
        /// <param name="conditionValue"></param>
        /// <param name="cOperator"></param>
        /// <returns></returns>
        public static string GetSimpleConditionSqlItem(string conditionField, string conditionValue, CompareOperator cOperator)
        {
            string strLast = "";
            switch (cOperator)
            {
                case CompareOperator.Equals:
                    strLast = string.Format(" = '{0}'", conditionValue);
                    break;
                case CompareOperator.NotEqualTo:
                    strLast = string.Format(" != '{0}'", conditionValue);
                    break;
                case CompareOperator.GreaterThan:
                    strLast = string.Format(" > '{0}'", conditionValue);
                    break;
                case CompareOperator.LessThan:
                    strLast = string.Format(" < '{0}'", conditionValue);
                    break;
                case CompareOperator.GreaterThanOrEqualTo:
                    strLast = string.Format(" >= '{0}'", conditionValue);
                    break;
                case CompareOperator.LessThanOrEqualTo:
                    strLast = string.Format(" <= '{0}'", conditionValue);
                    break;
                case CompareOperator.Like:
                    strLast = string.Format(" like '%{0}%'", conditionValue);
                    break;
                case CompareOperator.StartWith:
                    strLast = string.Format(" like '{0}%'", conditionValue);
                    break;
                case CompareOperator.EndWith:
                    strLast = string.Format(" like '%{0}'", conditionValue);
                    break;
                case CompareOperator.In:
                    strLast = string.Format(" in {0}", TwiIDsHelper.GetInFormatIDs(conditionValue));
                    break;
                case CompareOperator.NotIn:
                    strLast = string.Format(" not in {0}", TwiIDsHelper.GetInFormatIDs(conditionValue));
                    break;
                case CompareOperator.BetweenAnd:
                    strLast = string.Format(" between '{0}' and '{2}'", conditionValue.Split(',')[0], conditionValue.Split(',')[1]);
                    break;
                default: break;
            }
            return strLast == "" ? "" : string.Format(" {0}{1}", conditionField, strLast);
        }


        public static string GetSimpleConditionSqlItem(ref SimpleCondition condition, string parameterPrefix = "@")
        {
            string strLast = " = ";
            switch (condition.COperator)
            {
                case CompareOperator.Equals:
                    strLast = string.Format(" {0} {1} {2}{0} ", condition.Field, "=", parameterPrefix);
                    break;
                case CompareOperator.NotEqualTo:
                    strLast = string.Format(" {0} {1} {2}{0} ", condition.Field, "!=", parameterPrefix);
                    break;
                case CompareOperator.GreaterThan:
                    strLast = string.Format(" {0} {1} {2}{0} ", condition.Field, ">", parameterPrefix);
                    break;
                case CompareOperator.LessThan:
                    strLast = string.Format(" {0} {1} {2}{0} ", condition.Field, "<", parameterPrefix);
                    break;
                case CompareOperator.GreaterThanOrEqualTo:
                    strLast = string.Format(" {0} {1} {2}{0} ", condition.Field, ">=", parameterPrefix);
                    break;
                case CompareOperator.LessThanOrEqualTo:
                    strLast = string.Format(" {0} {1} {2}{0} ", condition.Field, "<=", parameterPrefix);
                    break;
                case CompareOperator.Like:
                    strLast = string.Format(" {0} {1} {2}{0} ", condition.Field, "like", parameterPrefix);
                    condition.Value = "%" + condition.Value + "%";
                    break;
                case CompareOperator.StartWith:
                    strLast = string.Format(" {0} {1} {2}{0} ", condition.Field, "like", parameterPrefix);
                    condition.Value += "%";
                    break;
                case CompareOperator.EndWith:
                    strLast = string.Format(" {0} {1} {2}{0} ", condition.Field, "=", parameterPrefix);
                    condition.Value = "%" + condition.Value;
                    break;
                case CompareOperator.In:
                    strLast = string.Format(" {0} {1} ({2}{0}) ", condition.Field, "in", parameterPrefix);
                    condition.Value = TwiIDsHelper.GetInFormatIDs(condition.Value, needParentheses: false);
                    break;
                case CompareOperator.NotIn:
                    strLast = string.Format(" {0} {1} ({2}{0}) ", condition.Field, "not in", parameterPrefix);
                    condition.Value = TwiIDsHelper.GetInFormatIDs(condition.Value, needParentheses: false);
                    break;
                case CompareOperator.BetweenAnd: //这个不太好处理，暂时只设一个参数吧,虽然有点风险，将就着先这样用下，以后改进
                    strLast = string.Format(" {0} between {1}{0} and '{2}'", condition.Field, "in", parameterPrefix, condition.Value.Split(',')[1]);
                    condition.Value = condition.Value.Split(',')[0];
                    break;
                default: break;
            }
            return strLast;
        }

    }
}
